jQuery(document).ready(function ($) {
	

	

	
    $(".myo-bp-pollbar").bind("click", function () {
    	
    	if(window.myobuddyout == 'yes'){
    		alert('You must be a member to vote');
    		return false;
    	}
			
		i = $(this).attr("data-myo-poll-id");
		opt = $(this).attr("data-myo-poll-opt");
		fbid = window.myobuddyuser;
		fname = window.myobuddyname;

		
		oldopt = window.oldopt;
		if(oldopt == null){
			oldopt = 'none';
		}

		myo_bp_ajax();
		
		});
	
		myo_bp_ajax = function(){
        
        var parento = window.parento;
		console.log("ID" + fbid);
		console.log("Name "+ fname);
		console.log("Parent" + parento);
	
        
        var j = {
            action: "myo_bp_polling_vote",
            poll: i,
            option: opt,
            oldopt: oldopt,
            fbook: fbid,
            fnamey: fname,
            parent: parento,
        };
        
        console.log("Posting....");
        var l = $.ajax({
            url: MyAjax.ajaxurl,
            type: "POST",
            data: j,
            dataType: "json",
        });
        l.done(function (c) {
            var d = c.total;
            
            $(".shaded-" + i).each(function (f) {

                var e = Math.round(100 * c.votes[f] / d);
                $(".shaded-" + f).css({"width":  e + "%"})
                

                if(c.votes[f] == 1){
                	votey = 'vote';
                }else{
                	votey = 'votes';
                };
                
                $("#myo-poll-" + i + "-facepile-" + f).html(c.facepile[f]);
                $(".myo-fb-vote-" + f + "-poll-" + i).html(c.votes[f] + " " + votey );
            });
            body = c.body;
            rlink = c.link;
            rtitle = c.rtitle;
        
            
            if(c.repo == 0){
       
            }
            
        });
        l.fail(function (d, c) {
            alert("Request failed: " + c)
        });
       }
		
		
		
		

    
 	return true;   
 
});